#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/select.h>

char * tfgets(char *s, int size, FILE *stream)
{
    char *str;
    fd_set read_set;
    struct timeval time;

    FD_ZERO(&read_set);
    FD_SET(STDIN_FILENO, &read_set);

    time.tv_sec = 5;
    time.tv_usec = 0;

    if ((select(STDIN_FILENO+1, &read_set, NULL, NULL, &time) > 0) 
        && (FD_ISSET(STDIN_FILENO, &read_set)))
            return fgets(s, size, stream);

    return NULL;
}
